<template>
  <div>
    <div class="block">
      <p>使用 scoped slot</p>
      <el-tree :data="data1"
               node-key="id"
               default-expand-all
               :expand-on-click-node="false">
        <span class="custom-tree-node"
              slot-scope="{ node, data }">
          <span>{{ data.label}}</span>
          <span>
            <el-button type="text"
                       size="mini"
                       class='el-icon-plus'
                       @click="() => append(data)">
            </el-button>
            <el-button type="text"
                       size="mini"
                       class='el-icon-delete'
                       @click="() => remove(node, data)">
            </el-button>
            <el-button type="text"
                       size="mini"
                       :disabled="data.sign==0"
                       class='el-icon-top'
                       @click="() => upNode(node, data)">
            </el-button>
            <!-- {{node.parent.data.children?node.parent.data.children.length:'33'}} -->
            <!-- {{node.parent.data.children?data.sign!=node.parent.data.children.length-1:data.sign!=data1.length-1}} -->
            <el-button type="text"
                       size="mini"
                       :disabled="node.parent.data.children?data.sign==node.parent.data.children.length-1:data.sign==data1.length-1"
                       class='el-icon-bottom'
                       @click="() => downNode(node, data)">
            </el-button>

          </span>
          <!-- :class="{'white': node.parent.parent || node.parent.data.children  ? node.parent.data.children[0].id == data.id : false }" -->
        </span>
      </el-tree>
    </div>
  </div>
</template>
<script>
// let id = 1000;
export default {
  data () {
    return {
      data1: [],
      temporaryId: ''
    }
  },
  mounted () {
    this.handleTreeData()
  },
  methods: {
    handleTreeData () {//获取tree数组//我只是这样写，这里应该写ajax请求数据
      const ssss = [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }];
      // console.log(ssss)
      this.recursion(ssss)
      this.data1 = ssss
    },
    recursion (data) {//递归下
      data.map((item, index) => {
        item.sign = index
        if (item.children && item.children.length > 0) {
          this.recursion(item.children)
        }
      })
      // console.warn(this.data1)
    },

    upNode (node, data) {
      this.$message('id为' + data.id + '的移动方式为向上');
      // console.warn('id为' + data.id + '的移动方式为向上')
      //把id和移动方式调用后端接口，接下来再次获取tree数据，例如this.handleTreeData()
      console.warn(node, data)

    },
    // 下移
    downNode (node, data) {
      this.$message('id为' + data.id + '的移动方式为向下')
      //把id和移动方式调用后端接口，接下来再次获取tree数据，例如this.handleTreeData()
      console.warn(node, data)

    },
    // 追加节点
    append (data) {
      console.warn(data)
      this.temporaryId = data.id//把点击的id存到临时id里面
      this.$prompt('请输入目录名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
        inputErrorMessage: '不能为空'
      }).then(({ value }) => {
        console.log('打印一下添加的数据', value);
        this.$message('id为' + this.temporaryId + '的增加节点，节点内容为' + value)
        //把id和内容调用后端接口，接下来再次获取tree数据，例如this.handleTreeData()

      }).catch(() => {

      });
    },
    remove (node, data) {
      console.warn(node, data)
      this.temporaryId = data.id//把点击的id存到临时id里面
      this.$confirm('此操作将删除该节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message('id为' + this.temporaryId + '的删除节点')
        //把id调用后端接口，接下来再次获取tree数据，例如this.handleTreeData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    }
  }
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.white {
  color: #f2f3f3 !important;
  cursor: not-allowed !important;
}
</style>